//
//  YCInterviewDisplayCell.m
//  YCSchool
//
//  Created by gmall on 16/5/5.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  预约考试界面 展示cell

#import "YCInterviewDisplayCell.h"

@interface YCInterviewDisplayCell()


/** 点击预约情况按钮要执行的代码块  */
@property (nonatomic, copy) interviewStatusButtonClickActionBlock interviewStatusClickBlock;

/** 背景视图  */
@property (nonatomic, strong) UIView *bgView;

/** 科目名称  */
@property (nonatomic, weak) UILabel *courseLabel;
/** 科目说明  */
@property (nonatomic, weak) UILabel *courseDescLabel;
/** 预约状态按钮 */
@property (nonatomic, weak) UIButton *interviewStatusButton;

@end


@implementation YCInterviewDisplayCell

- (void)setItemModel:(YCInterviewDisplayModel *)itemModel
{
    _itemModel = itemModel;
    self.courseLabel.text = itemModel.cname;
    self.courseDescLabel.text = itemModel.des;
    switch (itemModel.bookmark) {
        case InterviewStatusTypeEnable://立即预约
        {
            self.interviewStatusButton.enabled = YES;
            [self.interviewStatusButton setTitle:@"立即预约" forState:UIControlStateNormal];
            [self.interviewStatusButton setImage:[[UIImage alloc] init] forState:UIControlStateNormal];
            [self.interviewStatusButton setTitleColor:[UIColor yc_colorWithUInt:0xffffff] forState:UIControlStateNormal];
            self.interviewStatusButton.backgroundColor = [UIColor yc_colorWithUInt:0x00cc00];
            self.interviewStatusButton.titleLabel.font = [UIFont systemFontOfSize:12];
        }
            break;
            
        case InterviewStatusTypeDisable://不可预约
        {
            self.interviewStatusButton.enabled = NO;
            [self.interviewStatusButton setTitle:@"暂不可预约" forState:UIControlStateDisabled];
            [self.interviewStatusButton setImage:[[UIImage alloc] init] forState:UIControlStateDisabled];
            [self.interviewStatusButton setTitleColor:[UIColor yc_colorWithUInt:0xcccccc] forState:UIControlStateDisabled];
            self.interviewStatusButton.backgroundColor = [UIColor clearColor];
        }
            break;
            
        case InterviewStatusTypeSucceed://预约成功
        {
            self.interviewStatusButton.enabled = YES;
            [self.interviewStatusButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [self.interviewStatusButton setTitle:@"预约成功" forState:UIControlStateNormal];
            [self.interviewStatusButton setImage:[UIImage imageNamed:@"预约成功"] forState:UIControlStateNormal];
            self.interviewStatusButton.backgroundColor = [UIColor yc_colorWithUInt:0x00b4ff];
            self.interviewStatusButton.titleLabel.font = [UIFont systemFontOfSize:12];
        }
            break;
            
        case InterviewStatusTypeComplete://已完成
        {
            self.interviewStatusButton.enabled = NO;
            [self.interviewStatusButton setTitleColor:[UIColor yc_colorWithUInt:0xcccccc] forState:UIControlStateDisabled];
            [self.interviewStatusButton setTitle:@"已通过" forState:UIControlStateDisabled];
            [self.interviewStatusButton setImage:[UIImage imageNamed:@"已完成"] forState:UIControlStateDisabled];
            self.interviewStatusButton.backgroundColor = [UIColor clearColor];
            self.interviewStatusButton.titleLabel.font = [UIFont systemFontOfSize:13];
        }
            break;
            
        default:
            break;
    }
}

/**
 *  快速创建cell
 *
 *  @param tableView cell所在的表
 *  @param indexPath cell位置
 *  @param action    点击详细介绍按钮的操作
 *
 *  @return 返回cell对象
 */
+ (instancetype)tableViewCellWithTableView:(UITableView *)tableView withIndexPath:(NSIndexPath *)indexPath interviewStatusButtonActionBlock:(interviewStatusButtonClickActionBlock)action
{
    static NSString *identifier = @"interviewDisplayCell";
    YCInterviewDisplayCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[YCInterviewDisplayCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor yc_colorWithUInt:0xf7f7f7];
        cell.interviewStatusClickBlock = action;
    }
    return cell;
}



- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.bgView];
        
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(15);
            make.right.equalTo(self.contentView).offset(-15);
            make.top.equalTo(self.contentView).offset(5.5);
            make.bottom.equalTo(self.contentView).offset(-5.5);
        }];
    }
    return self;
}

- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = 5;
        _bgView.layer.masksToBounds = YES;
        
        // 科目名称
        UILabel *courseLabel = [[UILabel alloc] init];
        courseLabel.font = [UIFont systemFontOfSize:18];
        courseLabel.textAlignment = NSTextAlignmentLeft;
        courseLabel.text = @"科目一";
        courseLabel.textColor = [UIColor yc_colorWithUInt:0x4d4d4d];
        self.courseLabel = courseLabel;
        
        // 科目说明
        UILabel *courseDescLabel = [[UILabel alloc] init];
        courseDescLabel.font = [UIFont systemFontOfSize:12];
        courseDescLabel.textAlignment = NSTextAlignmentLeft;
        courseDescLabel.text = @"驾驶员理论考试";
        courseDescLabel.textColor = [UIColor yc_colorWithUInt:0x808080];
        self.courseDescLabel = courseDescLabel;
        
        // 预约状态按钮
        UIButton *interviewStatusButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [interviewStatusButton setTitle:@"预约成功" forState:UIControlStateNormal];
        [interviewStatusButton setTitleColor:[UIColor yc_colorWithUInt:0xcccccc] forState:UIControlStateNormal];
        [interviewStatusButton setTitle:@"已通过" forState:UIControlStateDisabled];
        [interviewStatusButton setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
        interviewStatusButton.titleLabel.font = [UIFont systemFontOfSize:12];
        interviewStatusButton.layer.cornerRadius = 3;
        interviewStatusButton.layer.masksToBounds = YES;
        interviewStatusButton.imageEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
        [interviewStatusButton addTarget:self action:@selector(interviewStatusButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        self.interviewStatusButton = interviewStatusButton;
        
        [_bgView addSubview:courseLabel];
        [_bgView addSubview:courseDescLabel];
        [_bgView addSubview:interviewStatusButton];
        
        // 布局
        [courseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_bgView).offset(28);
            make.bottom.equalTo(_bgView.mas_centerY).offset(-4);
        }];
        
        [courseDescLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(courseLabel);
            make.top.equalTo(courseLabel.mas_bottom).offset(8);
        }];
        
        [interviewStatusButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_bgView).offset(-25);
            make.centerY.equalTo(_bgView);
            make.height.equalTo(@23);//46px
            make.width.equalTo(@85);//180px
        }];
        
    }
    return _bgView;
}

// 点击按钮
- (void)interviewStatusButtonClick:(UIButton *)detailButton
{
    !self.interviewStatusClickBlock ? :self.interviewStatusClickBlock();
}

@end
